package cn.com.jenson.service.system.config;

import cn.com.jenson.common.AuthEntity;
import cn.com.jenson.common.KeyValue;
import cn.com.jenson.service.system.service.SystemInitService;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 自动加载修复方法
 */
@Component
public class AutoSystemConfig implements ApplicationRunner {

    @Resource
    private SystemInitService systemInitService;

    /**
     * 初始化<br/>
     *
     * @param args incoming application arguments
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        initAuth();
        systemInitService.init();
    }

    private void initAuth() {
        KeyValue[] keyValues = initAuthEntity();
        AuthEntity.authList = Arrays
                .stream(keyValues)
                .map(item -> new AuthEntity(item.getLabel(), Arrays.asList(item.getData())))
                .collect(Collectors.toList());
    }

    private KeyValue[] initAuthEntity() {
        return new KeyValue[]{
                new KeyValue("首页管理",new String[]{
                       "查看"
                }),
                new KeyValue("用户列表",new String[]{
                       "查看","新增","编辑","删除"
                }),
        };
    }
}
